#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUFSIZE 512
#define STRING1 "aaaaaa"
#define STRING2 "BBBBBB"
#define STRING3 "cccccc"

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Usage [%s]:  <port>\n", argv[0]);
        return 0;
    }
    int fd = socket(PF_INET, SOCK_DGRAM, 0);
    assert(fd != -1);
    
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(atoi(argv[1]));

    char buf[BUFSIZE];
    int rc; 
    rc = bind(fd, (struct sockaddr *)&addr, sizeof addr);
    assert(rc != -1);

    int len = sizeof(addr);
    while (1) {
        rc = recvfrom(fd, buf, sizeof buf, 0, (struct sockaddr *)&addr, (socklen_t *)&len);
        if (rc == 0)
            break;
        printf("recv from multicast: %s\n", buf);
    }
    close(fd);
    return 0;
}